<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Session;
use \think\Db;
class Moment extends Common
{
	public function index(){

		if(input('momentid')=='')

			$result = Db::name('moment')->alias('a')->join('user b','a.user_id=b.user_id')->order('moment_id')->select();
		else
			$result =  Db::name('moment')->where('moment_id',input('momentid'))->alias('a')->join('user b','a.user_id=b.user_id')->order('moment_id')->select();

		$this->assign('momentlist',$result);
		return $this->fetch();
	}

	public function momentcomment(){
		if(input('commentid')==''&&input('momentid')=='')
			$result = Db::name('momentcomment')->alias('a')->join('user b','a.user_id=b.user_id')->order('a.user_id')->select();
		else if(input('momentid')=='')
			$result = Db::name('momentcomment')->where('comment_id',input('commentid'))->alias('a')->join('user b','a.user_id=b.user_id')->order('a.user_id')->select();
		else
			$result = Db::name('momentcomment')->where('moment_id',input('momentid'))->alias('a')->join('user b','a.user_id=b.user_id')->order('a.user_id')->select();
		$this->assign('commentlist',$result);
		return $this->fetch();
	}

	public function momentmessage(){
		
		//私信列表
		$userid = input('userid');
		
		$sql = 'SELECT p.msg_id,p.send_user,p.receive_user,p.user_id2,count(p.msg_id)count,msg_content,msg_time,u.user_name FROM (select * from message where user_id = '.$userid.' order by msg_id desc) p inner join user u on u.user_id=p.user_id2 group by p.user_id2 order by p.msg_id desc';
		$result = Db::query($sql);

	
		//私信详情
		//SELECT p.msg_id,p.msg_content,p.send_user,p.receive_user,p.msg_time,u.`user_name` AS sender_name,u.user_headimage AS sender_image,uu.`user_name` AS receiver_name,uu.`user_headimage` AS receiver_image FROM message p INNER JOIN user u on u.user_id=p.send_user INNER JOIN user uu on uu.user_id=p.receive_user WHERE p.user_id=1 and p.user_id2=2 and p.`msg_status` !=3 ORDER BY p.msg_id desc
		$this->assign('user_id',$userid);
		$this->assign('messagelist',$result);
		return $this->fetch();
	}

	public function message(){
		$user1 = input('userid1');
		$user2 = input('userid2');
		$sql = 'SELECT p.msg_id,p.msg_content,p.send_user,p.receive_user,p.msg_time,u.`user_name` AS sender_name,u.user_headimage AS sender_image,uu.`user_name` AS receiver_name,uu.`user_headimage` AS receiver_image FROM message p INNER JOIN user u on u.user_id=p.send_user INNER JOIN user uu on uu.user_id=p.receive_user WHERE p.user_id='.$user1.' and p.user_id2= '.$user2.' and p.`msg_status` !=3 ORDER BY p.msg_id';
		$result = Db::query($sql);
		$this->assign('userid',$user1);
		$this->assign('messagelist',$result);
		return $this->fetch();
	}

	
}